﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
namespace JumbotCms.Extends.Translate
{
    public partial class _batchinsert : JumbotCms.API.AdminCenter
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Admin_Load("0000", "html");
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            HttpPostedFile oFile = this.uploadfile1.PostedFile; //得到要上传文件
            if (oFile != null && oFile.ContentLength > 0)
            {
                try
                {
                    string fileContentType = oFile.ContentType; //文件类型
                    string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower(); //上传文件的扩展名
                    string F_Type = fileExtension.Substring(1, fileExtension.Length - 1);
                    if (F_Type.ToLower() == "xls")//检测是否为xls
                    {
                        string DirectoryPath;
                        DirectoryPath = site.Dir + "_data/tempfiles";
                        string sFileName = "extends_translate_tmp" + fileExtension;
                        string FullPath = DirectoryPath + "/" + sFileName;
                        oFile.SaveAs(Server.MapPath(FullPath));
                        this.UploadMsg.InnerHtml = "上传成功";
                        Jumbot.DataOper.XlsControl XlsTool = new Jumbot.DataOper.XlsControl(Server.MapPath(FullPath));
                        DataTable dt = XlsTool.GetData("Sheet1");
                        string _content1, _content2;
                        int _success = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            _content1 = dt.Rows[i][0].ToString();
                            _content2 = dt.Rows[i][1].ToString();
                            if (_content1.Length > 1 && _content2.Length > 1)
                            {
                                doh.Reset();
                                doh.ConditionExpress = "content1=@content1";
                                doh.AddConditionParameter("@content1", _content1);
                                if (!doh.Exist("jcms_extends_translate"))
                                {
                                    doh.Reset();
                                    doh.AddFieldItem("content1", _content1);
                                    doh.AddFieldItem("content2", _content2);
                                    doh.AddFieldItem("State", 1);
                                    doh.Insert("jcms_extends_translate");
                                    _success++;
                                }
                            }
                        }
                        dt.Clear();
                        dt.Dispose();
                        FinalMessage(_success + "条记录导入成功", site.Dir + "admin/close.htm", 0);
                    }
                    else //文件类型不允许上传
                    {
                        this.UploadMsg.InnerHtml = F_Type + "不是标准Excel文件";
                    }
                }
                catch
                {
                    this.UploadMsg.InnerHtml = "因程序异常导致上传未成功";
                }
            }
            else
            {
                this.UploadMsg.InnerHtml = "请选择上传文件";
            }
        }
    }
}
